package com.hanlink.dsp.util;

/**
 * Created by zhb on 2017/9/17.
 */

public class FreqUtil {
    public static int parseProgressToFreq2(int progress) {
        int freq = 0;
        if (progress <= 28) {
            freq = Math.round(progress * 80 / 28f + 20);
        } else if (progress > 28 && progress <= 61) {
            freq = Math.round((progress - 28) * 900 / 33f + 100);
        } else if (progress > 61 && progress <= 94) {
            freq = Math.round((progress - 61) * 9000 / 33f + 1000);
        } else if (progress > 94 && progress <= 100) {
            freq = Math.round((progress - 94) * 10000 / 6f + 10000);
        }
        return freq;
    }

    public static int parseProgressToFreq(int progress) {
        int max = 199800;
        int freq = 0;
        float p_28 = 55944;
        float p_61 = 121878;
        float p_94 = 187812;

        if (progress <= p_28) {
            freq = Math.round(progress * 80 / p_28) + 20;
        } else if (progress > p_28) {
            freq = 100;
        }
        if (progress > p_28 && progress <= p_61) {
            float tempP = p_61 - p_28;
            float temP2 = progress - p_28;
            freq += Math.round(temP2 * 900 / tempP);
        } else if (progress > p_61) {
            freq = 1000;
        }
        if (progress > p_61 && progress <= p_94) {
            float tempP = p_94 - p_61;
            float temP2 = progress - p_61;
            freq += Math.round(temP2 * 9000 / tempP);
        } else if (progress > p_94) {
            freq = 10000;
        }
        if (progress > p_94 && progress <= max) {
            float tempP = max - p_94;
            float temP2 = progress - p_94;
            freq += Math.round(temP2 * 10000 / tempP);
        }
        return freq;
    }

    public static int parseFreqToProgress(int freq) {
        int max = 199800;
        int progress = 0;
        int p_28 = 55944;
        int p_61 = 121878;
        int p_94 = 187812;

        if (freq <= 100) {
            progress = Math.round((freq - 20) * p_28 / 80f);
        } else if (freq > 100) {
            progress = p_28;
        }
        if (freq > 100 && freq <= 1000) {
            int temP2 = p_61 - p_28;
            progress += Math.round((freq - 100) * temP2 / 900f);
        } else if (freq > 1000) {
            progress = p_61;
        }
        if (freq > 1000 && freq <= 10000) {
            int temP2 = p_94 - p_61;
            progress += Math.round((freq - 1000) * temP2 / 9000f);
        } else if (freq > 10000) {
            progress = p_94;
        }
        if (freq > 10000 && freq <= 20000) {
            int temP2 = max - p_94;
            progress += Math.round((freq - 10000) * temP2 / 10000f);
        }
        return progress;
    }

    public static int parseFreqToProgress2(int freq) {
        int progress = 0;
        if (freq <= 100) {
            progress = Math.round((freq - 20) * 28f / 80);
        } else if (freq > 100 && freq <= 1000) {
            progress = Math.round((freq - 100) * 33f / 900) + 28;
        } else if (freq > 1000 && freq <= 10000) {
            progress = Math.round((freq - 1000) * 33f / 9000) + 33 + 28;
        } else if (freq > 10000 && freq <= 20000) {
            progress = Math.round((freq - 10000) * 6f / 10000) + 33 + 33 + 28;
        }
        return progress;
    }
}
